#pragma once
#include "stdafx.h"
#include "Parse.h"

wstring Parse::StrToUnicode(string str)
{
	wstring result;
	int size;
	bool is_utf8 = FALSE;
	if (str.size() >= 3 && str[0] == -17 && str[1] == -69 && str[2] == -65)
	{
		is_utf8 = true;
		str = str.substr(3);
	}
	if (!is_utf8)
	{
		size = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
		if (size <= 0) return wstring();
		wchar_t* str_unicode = new wchar_t[size + 1];
		MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, str_unicode, size);
		result.assign(str_unicode);
		delete[] str_unicode;
	}
	else
	{
		size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
		if (size <= 0) return wstring();
		wchar_t* str_unicode = new wchar_t[size + 1];
		MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, str_unicode, size);
		result.assign(str_unicode);
		delete[] str_unicode;
	}
	return result;
}

string Parse::UnicodeToStr(wstring wstr)
{
	string result;
	int size{ 0 };
	size = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
	if (size <= 0) return string();
	char* str = new char[size + 1];
	WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, str, size, NULL, NULL);
	result.assign(str);
	delete[] str;
	return result;
}

int Parse::WstringToInt(wstring wstr)
{
	wstringstream wstream;
	int result;
	wstream << wstr;
	wstream >> result;
	wstream.clear();
	return result;
}

wstring Parse::IntToWstring(int intvalue)
{
	wostringstream oss;
	oss << intvalue;

	wstring wstr = oss.str();
	wcout << wstr << endl;
	return wstr;
}